#include "../DataBase_h/DataBaseConfig.h"
#include "../Error_h/error.h"
#include <QFile>
#include <QTextStream>
#include <QVector>

DataBaseConfig* DataBaseConfig::_instance = NULL;

DataBaseConfig::DataBaseConfig(){

}

DataBaseConfig* DataBaseConfig::GetInstance(){
    if(!_instance){
		_instance = new DataBaseConfig();
	}	
	return _instance;
}

int DataBaseConfig::ReadFile(QString filePath){
    QFile configFile(filePath);

    //打开失败
    if(!configFile.open(QIODevice::ReadOnly|QIODevice::Text)){
        return OPEN_CONFIG_FILE_ERROR;
    }

    QTextStream stream(&filePath);
    QVector<QString> lineStr;
    while(!stream.atEnd()){
        lineStr.append(stream.readLine());
    }

    int configNum = lineStr[0].toInt();

    //配置文件所写的值不足指定的数量时
    if(lineStr.size()<configNum){
        return CONFIG_NUM_NOT_ENOUGH_ERROR;
    }

    //逐一赋值
    for(int i = 0;i<configNum;++i){
        switch (i) {
        case 0:
            _ip = lineStr[i];
            break;
        case 1:
            _port = lineStr[i];
            break;
         case 2:
            _dataBaseName = lineStr[i];
            break;
         case 3:
            _userName = lineStr[i];
            break;
          case 4:
            _userPwd = lineStr[i];
            break;
        default:
            break;
        }
    }

    return NONE;
}

QString DataBaseConfig::GetIp() const{
	return _ip;
}

QString DataBaseConfig::GetPort() const{
	return _port;
}

QString DataBaseConfig::GetDataBaseName() const{
    return _dataBaseName;
}

QString DataBaseConfig::GetUserName() const{
    return _userName;
}

QString DataBaseConfig::GetUserPwd() const{
    return _userPwd;
}
